<!DOCTYPE html><html lang="zh-CN" data-theme="light"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0,viewport-fit=cover"><title>Docker备忘笔记 | LenlBlog</title><meta name="author" content="Lenl,lenl412@163.com"><meta name="copyright" content="Lenl"><meta name="format-detection" content="telephone=no"><meta name="theme-color" content="ffffff"><meta name="description" content="学习一下在linux上使用docker">
<meta property="og:type" content="article">
<meta property="og:title" content="Docker备忘笔记">
<meta property="og:url" content="https://lienle.gitee.io/notes/dockerNotes/index.html">
<meta property="og:site_name" content="LenlBlog">
<meta property="og:description" content="学习一下在linux上使用docker">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://z4a.net/images/2023/04/22/fcc1d07f3136fe8e818242a6534391c8.md.png">
<meta property="article:published_time" content="2024-04-06T13:17:09.000Z">
<meta property="article:modified_time" content="2024-04-27T13:18:46.787Z">
<meta property="article:author" content="Lenl">
<meta property="article:tag" content="Docker">
<meta property="article:tag" content="Linux">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://z4a.net/images/2023/04/22/fcc1d07f3136fe8e818242a6534391c8.md.png"><link rel="shortcut icon" href="/img/lenl.png"><link rel="canonical" href="https://lienle.gitee.io/notes/dockerNotes/index.html"><link rel="preconnect" href="//cdn.jsdelivr.net"/><link rel="preconnect" href="//hm.baidu.com"/><link rel="preconnect" href="//busuanzi.ibruce.info"/><meta name="baidu-site-verification" content="codeva-6HME93uF43"/><link rel="stylesheet" href="/css/index.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free/css/all.min.css" media="print" onload="this.media='all'"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fancyapps/ui/dist/fancybox/fancybox.min.css" media="print" onload="this.media='all'"><script>var _hmt = _hmt || [];
(function() {
  var hm = document.createElement("script");
  hm.src = "https://hm.baidu.com/hm.js?629608ec0c773bd9f96cb72389693ab9";
  var s = document.getElementsByTagName("script")[0]; 
  s.parentNode.insertBefore(hm, s);
})();
</script><script>const GLOBAL_CONFIG = { 
  root: '/',
  algolia: undefined,
  localSearch: {"path":"/search.xml","preload":false,"top_n_per_article":1,"unescape":false,"languages":{"hits_empty":"找不到您查询的内容：${query}","hits_stats":"共找到 ${hits} 篇文章"}},
  translate: undefined,
  noticeOutdate: undefined,
  highlight: {"plugin":"highlighjs","highlightCopy":true,"highlightLang":true,"highlightHeightLimit":false},
  copy: {
    success: '复制成功',
    error: '复制错误',
    noSupport: '浏览器不支持'
  },
  relativeDate: {
    homepage: false,
    post: false
  },
  runtime: '',
  dateSuffix: {
    just: '刚刚',
    min: '分钟前',
    hour: '小时前',
    day: '天前',
    month: '个月前'
  },
  copyright: {"limitCount":50,"languages":{"author":"作者: Lenl","link":"链接: ","source":"来源: LenlBlog","info":"著作权归作者所有。商业转载请联系作者获得授权，非商业转载请注明出处。"}},
  lightbox: 'fancybox',
  Snackbar: undefined,
  source: {
    justifiedGallery: {
      js: 'https://cdn.jsdelivr.net/npm/flickr-justified-gallery/dist/fjGallery.min.js',
      css: 'https://cdn.jsdelivr.net/npm/flickr-justified-gallery/dist/fjGallery.min.css'
    }
  },
  isPhotoFigcaption: true,
  islazyload: true,
  isAnchor: false,
  percent: {
    toc: true,
    rightside: false,
  }
}</script><script id="config-diff">var GLOBAL_CONFIG_SITE = {
  title: 'Docker备忘笔记',
  isPost: true,
  isHome: false,
  isHighlightShrink: true,
  isToc: true,
  postUpdate: '2024-04-27 21:18:46'
}</script><noscript><style type="text/css">
  #nav {
    opacity: 1
  }
  .justified-gallery img {
    opacity: 1
  }

  #recent-posts time,
  #post-meta time {
    display: inline !important
  }
</style></noscript><script>(win=>{
    win.saveToLocal = {
      set: function setWithExpiry(key, value, ttl) {
        if (ttl === 0) return
        const now = new Date()
        const expiryDay = ttl * 86400000
        const item = {
          value: value,
          expiry: now.getTime() + expiryDay,
        }
        localStorage.setItem(key, JSON.stringify(item))
      },

      get: function getWithExpiry(key) {
        const itemStr = localStorage.getItem(key)

        if (!itemStr) {
          return undefined
        }
        const item = JSON.parse(itemStr)
        const now = new Date()

        if (now.getTime() > item.expiry) {
          localStorage.removeItem(key)
          return undefined
        }
        return item.value
      }
    }
  
    win.getScript = url => new Promise((resolve, reject) => {
      const script = document.createElement('script')
      script.src = url
      script.async = true
      script.onerror = reject
      script.onload = script.onreadystatechange = function() {
        const loadState = this.readyState
        if (loadState && loadState !== 'loaded' && loadState !== 'complete') return
        script.onload = script.onreadystatechange = null
        resolve()
      }
      document.head.appendChild(script)
    })
  
    win.getCSS = (url,id = false) => new Promise((resolve, reject) => {
      const link = document.createElement('link')
      link.rel = 'stylesheet'
      link.href = url
      if (id) link.id = id
      link.onerror = reject
      link.onload = link.onreadystatechange = function() {
        const loadState = this.readyState
        if (loadState && loadState !== 'loaded' && loadState !== 'complete') return
        link.onload = link.onreadystatechange = null
        resolve()
      }
      document.head.appendChild(link)
    })
  
      win.activateDarkMode = function () {
        document.documentElement.setAttribute('data-theme', 'dark')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#0d0d0d')
        }
      }
      win.activateLightMode = function () {
        document.documentElement.setAttribute('data-theme', 'light')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', 'ffffff')
        }
      }
      const t = saveToLocal.get('theme')
    
          if (t === 'dark') activateDarkMode()
          else if (t === 'light') activateLightMode()
        
      const asideStatus = saveToLocal.get('aside-status')
      if (asideStatus !== undefined) {
        if (asideStatus === 'hide') {
          document.documentElement.classList.add('hide-aside')
        } else {
          document.documentElement.classList.remove('hide-aside')
        }
      }
    
    const detectApple = () => {
      if(/iPad|iPhone|iPod|Macintosh/.test(navigator.userAgent)){
        document.documentElement.classList.add('apple')
      }
    }
    detectApple()
    })(window)</script></head><body><!--include ./fullpage-loading.pug--><div id="loading-box"><div class="loading-left-bg"></div><div class="loading-right-bg"></div><div class="wizard-scene"><div class="wizard-objects"><div class="wizard-square"></div><div class="wizard-circle"></div><div class="wizard-triangle"></div></div><div class="wizard"><div class="wizard-body"></div><div class="wizard-right-arm"><div class="wizard-right-hand"></div></div><div class="wizard-left-arm"><div class="wizard-left-hand"></div></div><div class="wizard-head"><div class="wizard-beard"></div><div class="wizard-face"><div class="wizard-adds"></div></div><div class="wizard-hat"><div class="wizard-hat-of-the-hat"></div><div class="wizard-four-point-star --first"></div><div class="wizard-four-point-star --second"></div><div class="wizard-four-point-star --third"></div></div></div></div></div></div><script>const preloader = {
  endLoading: () => {
    document.body.style.overflow = '';
    document.getElementById('loading-box').classList.add("loaded")
  },
  initLoading: () => {
    document.body.style.overflow = 'hidden';
    document.getElementById('loading-box').classList.remove("loaded")
  }
}

preloader.initLoading()
window.addEventListener('load',()=> { preloader.endLoading() })
//避免加载时间过长
setTimeout(function(){preloader.endLoading();}, 3000);

if (false) {
  document.addEventListener('pjax:send', () => { preloader.initLoading() })
  document.addEventListener('pjax:complete', () => { preloader.endLoading() })
}</script><div id="sidebar"><div id="menu-mask"></div><div id="sidebar-menus"><div class="avatar-img is-center"><img src= "/img/hexo_loading.gif" data-lazy-src="/img/lenl.png" onerror="onerror=null;src='/img/friend_404.gif'" alt="avatar"/></div><div class="sidebar-site-data site-data is-center"><a href="/archives/"><div class="headline">文章</div><div class="length-num">17</div></a><a href="/tags/"><div class="headline">标签</div><div class="length-num">25</div></a><a href="/categories/"><div class="headline">分类</div><div class="length-num">8</div></a></div><hr/><div class="menus_items"><div class="menus_item"><a class="site-page" href="/"><i class="fa-fw fas fa-home"></i><span> 主页</span></a></div><div class="menus_item"><a class="site-page" href="/archives/"><i class="fa-fw fas fa-archive"></i><span> 存档</span></a></div><div class="menus_item"><a class="site-page" href="/tags/"><i class="fa-fw fas fa-tags"></i><span> 标签</span></a></div><div class="menus_item"><a class="site-page" href="/categories/"><i class="fa-fw fas fa-folder-open"></i><span> 分类</span></a></div><div class="menus_item"><a class="site-page group" href="javascript:void(0);"><i class="fa-fw fas fa-list"></i><span> 分享</span><i class="fas fa-chevron-down"></i></a><ul class="menus_item_child"><li><a class="site-page child" href="/collect/"><i class="fa-fw fas fa-book"></i><span> 大佬们の干货</span></a></li><li><a class="site-page child" href="/resources/"><i class="fa-fw fab fa-dropbox"></i><span> 屯屯鼠の资源</span></a></li></ul></div><div class="menus_item"><a class="site-page" href="/link/"><i class="fa-fw fas fa-link"></i><span> 友链</span></a></div><div class="menus_item"><a class="site-page" href="/about/"><i class="fa-fw fas fa-heart"></i><span> 关于</span></a></div></div></div></div><div class="post" id="body-wrap"><header class="post-bg fixed" id="page-header" style="background-image: url('https://z4a.net/images/2023/04/22/fcc1d07f3136fe8e818242a6534391c8.md.png')"><nav id="nav"><span id="blog-info"><a href="/" title="LenlBlog"><img class="site-icon" src= "/img/hexo_loading.gif" data-lazy-src="/img/lenl.png"/><span class="site-name">LenlBlog</span></a></span><div id="menus"><div id="search-button"><a class="site-page social-icon search" href="javascript:void(0);"><i class="fas fa-search fa-fw"></i><span> 搜索</span></a></div><div class="menus_items"><div class="menus_item"><a class="site-page" href="/"><i class="fa-fw fas fa-home"></i><span> 主页</span></a></div><div class="menus_item"><a class="site-page" href="/archives/"><i class="fa-fw fas fa-archive"></i><span> 存档</span></a></div><div class="menus_item"><a class="site-page" href="/tags/"><i class="fa-fw fas fa-tags"></i><span> 标签</span></a></div><div class="menus_item"><a class="site-page" href="/categories/"><i class="fa-fw fas fa-folder-open"></i><span> 分类</span></a></div><div class="menus_item"><a class="site-page group" href="javascript:void(0);"><i class="fa-fw fas fa-list"></i><span> 分享</span><i class="fas fa-chevron-down"></i></a><ul class="menus_item_child"><li><a class="site-page child" href="/collect/"><i class="fa-fw fas fa-book"></i><span> 大佬们の干货</span></a></li><li><a class="site-page child" href="/resources/"><i class="fa-fw fab fa-dropbox"></i><span> 屯屯鼠の资源</span></a></li></ul></div><div class="menus_item"><a class="site-page" href="/link/"><i class="fa-fw fas fa-link"></i><span> 友链</span></a></div><div class="menus_item"><a class="site-page" href="/about/"><i class="fa-fw fas fa-heart"></i><span> 关于</span></a></div></div><div id="toggle-menu"><a class="site-page" href="javascript:void(0);"><i class="fas fa-bars fa-fw"></i></a></div></div></nav><div id="post-info"><h1 class="post-title">Docker备忘笔记</h1><div id="post-meta"><div class="meta-firstline"><span class="post-meta-date"><i class="far fa-calendar-alt fa-fw post-meta-icon"></i><span class="post-meta-label">发表于</span><time class="post-meta-date-created" datetime="2024-04-06T13:17:09.000Z" title="发表于 2024-04-06 21:17:09">2024-04-06</time><span class="post-meta-separator">|</span><i class="fas fa-history fa-fw post-meta-icon"></i><span class="post-meta-label">更新于</span><time class="post-meta-date-updated" datetime="2024-04-27T13:18:46.787Z" title="更新于 2024-04-27 21:18:46">2024-04-27</time></span><span class="post-meta-categories"><span class="post-meta-separator">|</span><i class="fas fa-inbox fa-fw post-meta-icon"></i><a class="post-meta-categories" href="/categories/%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0/">学习笔记</a></span></div><div class="meta-secondline"><span class="post-meta-separator">|</span><span class="post-meta-wordcount"><i class="far fa-file-word fa-fw post-meta-icon"></i><span class="post-meta-label">字数总计:</span><span class="word-count">4.6k</span><span class="post-meta-separator">|</span><i class="far fa-clock fa-fw post-meta-icon"></i><span class="post-meta-label">阅读时长:</span><span>17分钟</span></span><span class="post-meta-separator">|</span><span class="post-meta-pv-cv" id="" data-flag-title="Docker备忘笔记"><i class="far fa-eye fa-fw post-meta-icon"></i><span class="post-meta-label">阅读量:</span><span id="busuanzi_value_page_pv"><i class="fa-solid fa-spinner fa-spin"></i></span></span><span class="post-meta-separator">|</span><span class="post-meta-commentcount"><i class="far fa-comments fa-fw post-meta-icon"></i><span class="post-meta-label">评论数:</span><a href="/notes/dockerNotes/#post-comment" itemprop="discussionUrl"><span class="valine-comment-count" data-xid="/notes/dockerNotes/" itemprop="commentCount"><i class="fa-solid fa-spinner fa-spin"></i></span></a></span></div></div></div></header><main class="layout" id="content-inner"><div id="post"><article class="post-content" id="article-container"><div class="note info simple"><p>记录一下在Linux(CentOS7)上使用Docker容器的注意事项以及部署相关应用环境的记录。以便于下次使用时作为参考和备忘。</p>
</div>
<h2 id="Docker安装">Docker安装</h2>
<h3 id="卸载已安装的docker">卸载已安装的docker</h3>
<figure class="highlight bash"><table><tr><td class="code"><pre><span class="line">sudo yum remove docker\</span><br><span class="line">	docker-client\</span><br><span class="line">	docker-client-latest\</span><br><span class="line">	docker-common\</span><br><span class="line">	docker-latest\</span><br><span class="line">	docker-latest-logrotate\</span><br><span class="line">	docker-logrotate\</span><br><span class="line">	docker-engine</span><br></pre></td></tr></table></figure>
<h3 id="Linux工具安装">Linux工具安装</h3>
<figure class="highlight bash"><table><tr><td class="code"><pre><span class="line">sudo yum install -y yum-utils</span><br><span class="line">sudo yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo</span><br></pre></td></tr></table></figure>
<h3 id="docker安装">docker安装</h3>
<figure class="highlight bash"><table><tr><td class="code"><pre><span class="line">sudo yum install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin</span><br></pre></td></tr></table></figure>
<h3 id="启动docker">启动docker</h3>
<figure class="highlight bash"><table><tr><td class="code"><pre><span class="line">sudo systemctl <span class="built_in">enable</span> docker</span><br><span class="line">sudo systemctl start docker</span><br></pre></td></tr></table></figure>
<h3 id="测试docker">测试docker</h3>
<figure class="highlight bash"><table><tr><td class="code"><pre><span class="line">docker -v</span><br><span class="line">sudo docker run hello-world</span><br></pre></td></tr></table></figure>
<h3 id="docker镜像加速">docker镜像加速</h3>
<p>阿里云登录后搜索 <code>容器镜像服务</code> - <code>镜像工具</code> - <code>镜像加速器</code> ，按下面 <code>centos</code> 的配置文档操作。</p>
<figure class="highlight bash"><table><tr><td class="code"><pre><span class="line">sudo <span class="built_in">mkdir</span> -p /etc/docker</span><br><span class="line">sudo <span class="built_in">tee</span> /etc/docker/daemon.json &lt;&lt;-<span class="string">&#x27;EOF&#x27;</span></span><br><span class="line">&#123;</span><br><span class="line">  <span class="string">&quot;registry-mirrors&quot;</span>: [<span class="string">&quot;https://ss4awp7v.mirror.aliyuncs.com&quot;</span>]</span><br><span class="line">&#125;</span><br><span class="line">EOF</span><br><span class="line">sudo systemctl daemon-reload</span><br><span class="line">sudo systemctl restart docker</span><br></pre></td></tr></table></figure>
<h3 id="运行gitlab容器体验">运行gitlab容器体验</h3>
<figure class="highlight bash"><table><tr><td class="code"><pre><span class="line">docker run -d -p 8080 --hostname gitlab.example.com twang2218/gitlab-ce-zh:11.1</span><br></pre></td></tr></table></figure>
<p>完成后访问对应ip即可。</p>
<h3 id="docker帮助文档">docker帮助文档</h3>
<figure class="highlight bash"><table><tr><td class="code"><pre><span class="line">docker --<span class="built_in">help</span></span><br></pre></td></tr></table></figure>
<p>若查看具体命令的详细选项，就加上这个命令，如</p>
<figure class="highlight bash"><table><tr><td class="code"><pre><span class="line">docker ps --<span class="built_in">help</span></span><br></pre></td></tr></table></figure>
<h2 id="容器和镜像">容器和镜像</h2>
<h3 id="镜像">镜像</h3>
<h4 id="搜索镜像">搜索镜像</h4>
<figure class="highlight bash"><table><tr><td class="code"><pre><span class="line">docker search xxx</span><br></pre></td></tr></table></figure>
<p>更好的是访问 <code>docker-hub</code> 在网页查询。</p>
<h4 id="下载镜像">下载镜像</h4>
<figure class="highlight bash"><table><tr><td class="code"><pre><span class="line">docker pull [镜像仓库地址/]镜像名[:标签]</span><br></pre></td></tr></table></figure>
<p>镜像仓库地址和标签可选。</p>
<p>若无镜像地址，则会从默认镜像仓库中下载镜像。</p>
<p>若无标签，默认下载的是latest版本。</p>
<p>如：</p>
<figure class="highlight bash"><table><tr><td class="code"><pre><span class="line"><span class="comment"># 下载mysql镜像latest版本</span></span><br><span class="line">docker pull mysql:latest</span><br><span class="line"><span class="comment"># 下载nginx镜像</span></span><br><span class="line">docker pull nginx</span><br><span class="line"><span class="comment"># 下载mysql镜像5.7标签的镜像</span></span><br><span class="line">docker pull mysql:5.7</span><br></pre></td></tr></table></figure>
<h4 id="列出镜像信息">列出镜像信息</h4>
<figure class="highlight bash"><table><tr><td class="code"><pre><span class="line">docker images [选项]</span><br></pre></td></tr></table></figure>
<p>探索：</p>
<figure class="highlight bash"><table><tr><td class="code"><pre><span class="line">docker images --<span class="built_in">help</span> <span class="comment"># 查看选项</span></span><br><span class="line"><span class="comment"># 列出镜像的镜像id</span></span><br><span class="line">docker images -q</span><br><span class="line"><span class="comment"># 所有的镜像名为“mysql” 的镜像id</span></span><br><span class="line">docker images --f=reference=<span class="string">&#x27;mysql&#x27;</span> -q</span><br></pre></td></tr></table></figure>
<h4 id="删除镜像">删除镜像</h4>
<figure class="highlight bash"><table><tr><td class="code"><pre><span class="line">docker rmi 镜像<span class="built_in">id</span></span><br></pre></td></tr></table></figure>
<h3 id="Docker操作常规流程">Docker操作常规流程</h3>
<h4 id="列出容器信息">列出容器信息</h4>
<figure class="highlight plaintext"><table><tr><td class="code"><pre><span class="line">docker ps [选项]</span><br></pre></td></tr></table></figure>
<p>列出所有容器，无论是否运行</p>
<figure class="highlight bash"><table><tr><td class="code"><pre><span class="line">docker ps -a</span><br></pre></td></tr></table></figure>
<p>列出所有退出状态的容器</p>
<figure class="highlight bash"><table><tr><td class="code"><pre><span class="line">docker ps -f=status=exited</span><br></pre></td></tr></table></figure>
<p>列出所有退出状态容器的id</p>
<figure class="highlight bash"><table><tr><td class="code"><pre><span class="line">docker ps -f=status=exited -q </span><br></pre></td></tr></table></figure>
<p>容器继续运行的条件：</p>
<p>docker容器运行必须有一个前台进程，如果没有则会被认为容器空闲，自行退出。</p>
<h4 id="创建并运行容器">创建并运行容器</h4>
<figure class="highlight bash"><table><tr><td class="code"><pre><span class="line">docker run [OPTIONS] IMAGE [COMMAND] [ARG...]</span><br></pre></td></tr></table></figure>
<p>其中:</p>
<p>OPTIONS：选项，需要重点关注-d,-p,-v,-e,–restart 。</p>
<p>IMAGE：镜像信息，推荐使用镜像名:TAG的写法</p>
<p>COMMAND：创建容器后要执行的命令</p>
<p>ARG…：COMMAND命令的参数</p>
<p>例如：</p>
<figure class="highlight bash"><table><tr><td class="code"><pre><span class="line">docker run nginx:latest</span><br><span class="line">docker run nginx:latest <span class="built_in">ls</span></span><br><span class="line">docker run nginx:latest <span class="built_in">ls</span> -a</span><br><span class="line">docker run -d nginx:latest <span class="built_in">ls</span> -a</span><br></pre></td></tr></table></figure>
<h5 id="运行方式">运行方式</h5>
<ol>
<li class="lvl-3">
<p>后台运行</p>
</li>
</ol>
<figure class="highlight bash"><table><tr><td class="code"><pre><span class="line">docker run -d nginx:latest</span><br></pre></td></tr></table></figure>
<ol start="2">
<li class="lvl-3">
<p>交互式运行</p>
</li>
</ol>
<figure class="highlight bash"><table><tr><td class="code"><pre><span class="line">docker run -it nginx:latest bash</span><br><span class="line"><span class="comment"># -i 以交互模式运行容器，通常与-t同时使用</span></span><br><span class="line"><span class="comment"># -t 启动容器后，为容器分配一个命令行，通常与-i同时使用</span></span><br><span class="line"><span class="comment"># exit 退出</span></span><br></pre></td></tr></table></figure>
<ol start="3">
<li class="lvl-3">
<p>默认运行方式</p>
</li>
</ol>
<h4 id="删除容器">删除容器</h4>
<figure class="highlight bash"><table><tr><td class="code"><pre><span class="line">docker <span class="built_in">rm</span> [选项] [容器<span class="built_in">id</span>或容器名...]</span><br><span class="line"><span class="comment"># 如</span></span><br><span class="line">docker <span class="built_in">rm</span> 045e3d43893d</span><br><span class="line"><span class="comment"># 多个容器用空格隔开</span></span><br></pre></td></tr></table></figure>
<p>运行中的容器不能直接删除，但是可以强制删除。</p>
<figure class="highlight bash"><table><tr><td class="code"><pre><span class="line">docker <span class="built_in">rm</span> -f 045e3d43893d</span><br></pre></td></tr></table></figure>
<p>删除所有容器</p>
<figure class="highlight plaintext"><table><tr><td class="code"><pre><span class="line">前置知识：</span><br><span class="line">$()命令替换可以将()中的命令输出结果作为字符串使用，如</span><br><span class="line">ls $(pwd)  =  ls /root</span><br></pre></td></tr></table></figure>
<p>所以用命令拿到所有镜像id后替换命令</p>
<figure class="highlight bash"><table><tr><td class="code"><pre><span class="line">docker <span class="built_in">rm</span> -f $(docker ps -a -q)</span><br></pre></td></tr></table></figure>
<p>删除所有非运行状态的容器</p>
<figure class="highlight bash"><table><tr><td class="code"><pre><span class="line">docker <span class="built_in">rm</span> $(docker ps -f=status=exited -q)</span><br></pre></td></tr></table></figure>
<h4 id="进入容器执行命令">进入容器执行命令</h4>
<figure class="highlight bash"><table><tr><td class="code"><pre><span class="line">docker <span class="built_in">exec</span> [选项] 容器ID或容器名 命令 [参数...]</span><br></pre></td></tr></table></figure>
<p>以交互式方式进入容器内部进行命令执行。</p>
<figure class="highlight bash"><table><tr><td class="code"><pre><span class="line">docker <span class="built_in">exec</span> -it 容器ID bash</span><br></pre></td></tr></table></figure>
<p>然后测试nginx是否启动。</p>
<figure class="highlight bash"><table><tr><td class="code"><pre><span class="line">curl http://127.0.0.1</span><br></pre></td></tr></table></figure>
<p>退出容器运行 <code>exit</code> 即可。</p>
<h4 id="查看容器日志">查看容器日志</h4>
<figure class="highlight bash"><table><tr><td class="code"><pre><span class="line">docker logs [选项] 容器ID或容器名</span><br></pre></td></tr></table></figure>
<p>但是日志若需要一些特殊的查看方式，则需要加一些选项。</p>
<figure class="highlight bash"><table><tr><td class="code"><pre><span class="line"><span class="comment"># 持续显示日志</span></span><br><span class="line">docker logs -f 容器ID或容器名</span><br><span class="line"><span class="comment"># 查看最新的n条</span></span><br><span class="line">docker logs -n n 容器ID或容器名</span><br></pre></td></tr></table></figure>
<h4 id="容器文件拷贝">容器文件拷贝</h4>
<p>使用docker cp命令实现容器和宿主机之间文件和目录的相互拷贝。</p>
<figure class="highlight bash"><table><tr><td class="code"><pre><span class="line"><span class="comment"># 将容器中文件拷贝到宿主机</span></span><br><span class="line">docker <span class="built_in">cp</span> [OPTIONS] CONTAINER:SRC_PATH DEST_PATH</span><br><span class="line"><span class="comment"># 把宿主机文件拷贝到容器中</span></span><br><span class="line">docker <span class="built_in">cp</span> [OPTIONS] SRC_PATH CONTAINER:DEST_PATH</span><br></pre></td></tr></table></figure>
<h4 id="停止与运行容器">停止与运行容器</h4>
<figure class="highlight bash"><table><tr><td class="code"><pre><span class="line"><span class="comment"># 停止容器</span></span><br><span class="line">docker stop 容器ID或容器名...</span><br><span class="line"><span class="comment"># 启动已停止容器</span></span><br><span class="line">docker start 容器ID或容器名</span><br></pre></td></tr></table></figure>
<h3 id="run命令详解">run命令详解</h3>
<h4 id="常用选项">常用选项</h4>
<h5 id="p-端口映射">-p 端口映射</h5>
<figure class="highlight bash"><table><tr><td class="code"><pre><span class="line">docker run -p 宿主机端口:容器端口 镜像名 <span class="comment"># 发布一个端口</span></span><br><span class="line">docker run -p 宿主机端口1:容器端口1 -p 宿主机端口2:容器端口2 镜像名 <span class="comment"># 发布多个端口</span></span><br></pre></td></tr></table></figure>
<p>怎么知道映射哪些端口？ docker hub介绍或者docker file (如EXPOSE 80)</p>
<h5 id="v-数据卷">-v 数据卷</h5>
<p>实现容器和宿主机之间数据的同步（容器中文件的从持久化或宿主机与容器之间文件的数据共享）。</p>
<p>概念：将宿主机目录或文件挂在到容器中，实现主机和容器之间的数据共享和持久化。</p>
<p>用法：</p>
<figure class="highlight bash"><table><tr><td class="code"><pre><span class="line">docker run -v 宿主机目录:容器目录[:读写权限] 镜像名</span><br><span class="line"><span class="comment"># 读写权限为 ro 或 rw，默认是 rw </span></span><br></pre></td></tr></table></figure>
<p>如何知道哪些位置需要挂在数据卷：docker hub介绍或者docker file(VOLUME xxx)</p>
<p>如：</p>
<figure class="highlight bash"><table><tr><td class="code"><pre><span class="line">docker run -d -p 80:80 -v /home/cache:/usr/share/nginx/html nginx:latest</span><br></pre></td></tr></table></figure>
<h5 id="e设置环境变量">-e设置环境变量</h5>
<p>用法：</p>
<figure class="highlight bash"><table><tr><td class="code"><pre><span class="line">docker run -e 变量名=变量值 镜像名</span><br></pre></td></tr></table></figure>
<p>需要设置的变量依然是看DockerHub的介绍或docker file(如MYSQL_ROOT_PASSWORD)</p>
<p>练习：</p>
<figure class="highlight plaintext"><table><tr><td class="code"><pre><span class="line">后台运行一个mysql5.7的容器</span><br><span class="line">要求容器中的mysql可以被外部连接</span><br><span class="line">mysql容器中的数据要持久化存储，不能因为容器删除而丢失</span><br><span class="line">mysql中的用户密码设置为lenladmin</span><br></pre></td></tr></table></figure>
<p>命令如下：</p>
<figure class="highlight bash"><table><tr><td class="code"><pre><span class="line">docker run -d -p 3306:3306 -v /home/mysql_cache:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=lenladmin mysql:5.7</span><br></pre></td></tr></table></figure>
<h5 id="–name容器命名">–name容器命名</h5>
<p>方便帮助我们更快识别出容器的作用，而不用使用无意义的容器id。</p>
<p>用法：</p>
<figure class="highlight bash"><table><tr><td class="code"><pre><span class="line">docker run --name 需要定义的容器名 镜像名</span><br></pre></td></tr></table></figure>
<p>如：</p>
<figure class="highlight bash"><table><tr><td class="code"><pre><span class="line">docker run -d --name mynginx  -p 80:80 -v /home/cache:/usr/share/nginx/html nginx:latest</span><br></pre></td></tr></table></figure>
<h5 id="–restart重启策略">–restart重启策略</h5>
<p>宿主机重启时自动重启容器</p>
<p>重启策略如下：</p>
<ol>
<li class="lvl-3">
<p>no：容器退出时不会自动重启；</p>
</li>
<li class="lvl-3">
<p>always：总是会在退出后重启；</p>
</li>
<li class="lvl-3">
<p>on-failure[:max retries]：容器仅在非正常退出时重启，可以指定最大重试次数；</p>
</li>
<li class="lvl-3">
<p>unless-stopped：容器会在退出后自动重启，除非手动停止了容器。</p>
</li>
</ol>
<p>默认策略为no</p>
<p>如：</p>
<figure class="highlight bash"><table><tr><td class="code"><pre><span class="line">docker run -d --name mynginx --restart unless-stopped  -p 80:80 -v /home/cache:/usr/share/nginx/html nginx:latest</span><br></pre></td></tr></table></figure>
<h3 id="其他镜像和容器命令">其他镜像和容器命令</h3>
<h4 id="镜像-2">镜像</h4>
<h5 id="查看镜像详细信息">查看镜像详细信息</h5>
<figure class="highlight bash"><table><tr><td class="code"><pre><span class="line">docker image inspect 镜像ID</span><br></pre></td></tr></table></figure>
<h4 id="容器">容器</h4>
<h5 id="查看容器内进程情况">查看容器内进程情况</h5>
<figure class="highlight bash"><table><tr><td class="code"><pre><span class="line">docker top CONTAINER[ps OPTIONS]</span><br></pre></td></tr></table></figure>
<h5 id="查看容器详细信息">查看容器详细信息</h5>
<figure class="highlight bash"><table><tr><td class="code"><pre><span class="line">docker inspect 容器ID或容器名</span><br></pre></td></tr></table></figure>
<h3 id="数据卷高级">数据卷高级</h3>
<h4 id="设置数据卷">设置数据卷</h4>
<h5 id="绝对路径">绝对路径</h5>
<p>同上部分记录的设置方式</p>
<figure class="highlight bash"><table><tr><td class="code"><pre><span class="line">docker run -v 宿主机目录:容器目录[:读写权限] 镜像名</span><br></pre></td></tr></table></figure>
<h5 id="别名">别名</h5>
<p>可以直接使用数据卷别名作为宿主机的目录使用，如果别名不存在，Docker会自动创建对应的数据卷。</p>
<figure class="highlight bash"><table><tr><td class="code"><pre><span class="line">docker run -v 数据卷别名:容器目录[:读写权限] 镜像名</span><br></pre></td></tr></table></figure>
<p>Docker自动创建的数据卷在 <code>/var/lib/docker/volumes/nginxHtml/_data</code></p>
<h4 id="列出所有数据卷">列出所有数据卷</h4>
<figure class="highlight bash"><table><tr><td class="code"><pre><span class="line">docker volume <span class="built_in">ls</span></span><br></pre></td></tr></table></figure>
<h4 id="查看数据卷的详细信息">查看数据卷的详细信息</h4>
<figure class="highlight bash"><table><tr><td class="code"><pre><span class="line">docker volume inspect 数据卷别名</span><br></pre></td></tr></table></figure>
<h4 id="创建数据卷">创建数据卷</h4>
<figure class="highlight bash"><table><tr><td class="code"><pre><span class="line">docker volume create 数据卷名</span><br></pre></td></tr></table></figure>
<p>会自动创建数据卷，位置可用 <code>inspect</code> 进行查询。</p>
<h4 id="删除数据卷">删除数据卷</h4>
<figure class="highlight bash"><table><tr><td class="code"><pre><span class="line">docker volume <span class="built_in">rm</span> 数据卷名</span><br></pre></td></tr></table></figure>
<p>已使用的数据卷无法进行删除，需要停止容器运行再删除。</p>
<h2 id="项目部署案例-基础版本">项目部署案例-基础版本</h2>
<h3 id="环境准备">环境准备</h3>
<h4 id="mysql">mysql</h4>
<p>要求：能用的5.7版本的mysql容器</p>
<p>分析：</p>
<ol>
<li class="lvl-3">
<p>使用的镜像是mysql5.7</p>
</li>
<li class="lvl-3">
<p>后台运行 -d</p>
</li>
<li class="lvl-3">
<p>数据需要持久化存储 -v</p>
</li>
<li class="lvl-3">
<p>开放3306端口 -p</p>
</li>
<li class="lvl-3">
<p>设置root密码 -e (–env)</p>
</li>
<li class="lvl-3">
<p>停止后自动重启 --restart</p>
</li>
<li class="lvl-3">
<p>容器命名为 bqMysql</p>
</li>
</ol>
<p>命令：</p>
<figure class="highlight bash"><table><tr><td class="code"><pre><span class="line">docker run -d \</span><br><span class="line">-v mysqlData:/var/lib/mysql \</span><br><span class="line">-p 3306:3306 \</span><br><span class="line">-e MYSQL_ROOT_PASSWORD=root \</span><br><span class="line">--restart always \</span><br><span class="line">--name bqMysql \</span><br><span class="line">mysql:8.0.29 \</span><br><span class="line">--lower_case_table_names=1</span><br><span class="line"><span class="comment"># 设置不分区大小写</span></span><br></pre></td></tr></table></figure>
<p>注意：</p>
<p>mysql8.0以上更改了密码加密规则，需要进行更改：</p>
<p>首先使用以下命令进入MySQL的docker容器</p>
<figure class="highlight bash"><table><tr><td class="code"><pre><span class="line">docker <span class="built_in">exec</span> -it bqMysql bash</span><br></pre></td></tr></table></figure>
<p>然后登录MySQL</p>
<figure class="highlight plaintext"><table><tr><td class="code"><pre><span class="line">mysql -uroot -proot</span><br></pre></td></tr></table></figure>
<p>然后运行以下SQL即可</p>
<figure class="highlight csharp"><table><tr><td class="code"><pre><span class="line">alter user <span class="string">&#x27;root&#x27;</span>@<span class="string">&#x27;%&#x27;</span> identified <span class="keyword">by</span> <span class="string">&#x27;root&#x27;</span> password expire never;</span><br><span class="line">alter user <span class="string">&#x27;root&#x27;</span>@<span class="string">&#x27;%&#x27;</span> identified <span class="keyword">with</span> mysql_native_password <span class="keyword">by</span> <span class="string">&#x27;root&#x27;</span>; <span class="meta"># by后跟的是密码</span></span><br><span class="line">flush privileges;</span><br></pre></td></tr></table></figure>
<p>注意配置说明是否在意表名大小写限制。</p>
<h4 id="redis">redis</h4>
<p>要求：能用的7版本的redis容器</p>
<p>分析：</p>
<ol>
<li class="lvl-3">
<p>redis7</p>
</li>
<li class="lvl-3">
<p>后台运行 -d</p>
</li>
<li class="lvl-3">
<p>开启redis的时候开启aof持久化</p>
</li>
<li class="lvl-3">
<p>数据需要持久化存储 -v</p>
</li>
<li class="lvl-3">
<p>开放端口6379 -p</p>
</li>
<li class="lvl-3">
<p>重启策略 --restart</p>
</li>
<li class="lvl-3">
<p>容器命名为bqRedis</p>
</li>
</ol>
<p>前置知识：开启aof持久化 redis-sever --appendonly yes</p>
<p>命令：</p>
<figure class="highlight bash"><table><tr><td class="code"><pre><span class="line">docker run -d \</span><br><span class="line">-v redisData:/data \</span><br><span class="line">-p 6379:6379 \</span><br><span class="line">--restart always \</span><br><span class="line">--name bqRedis \</span><br><span class="line">redis:7.0 redis-server --appendonly <span class="built_in">yes</span></span><br></pre></td></tr></table></figure>
<h3 id="应用部署">应用部署</h3>
<h4 id="后端应用">后端应用</h4>
<p>要求：使用openJDK 1.8.0_111去运行SpringBoot服务，保证服务正常使用。</p>
<p>分析：</p>
<ol>
<li class="lvl-3">
<p>镜像：java: openjdk-8u111</p>
</li>
<li class="lvl-3">
<p>开放端口 -p 9988</p>
</li>
<li class="lvl-3">
<p>后台运行 -d</p>
</li>
<li class="lvl-3">
<p>数据卷同步jar包到容器 -v /usr/wzbq:/usr/wzbq</p>
</li>
<li class="lvl-3">
<p>重启策略 --restart always \</p>
</li>
<li class="lvl-3">
<p>重命名 jdk8111</p>
</li>
<li class="lvl-3">
<p>网络联通 --network</p>
</li>
</ol>
<p>命令：</p>
<figure class="highlight bash"><table><tr><td class="code"><pre><span class="line">docker run \</span><br><span class="line">-d \</span><br><span class="line">-p 9988:9988 \</span><br><span class="line">-v /usr/wzbq:/usr/wzbq \</span><br><span class="line">--restart always \</span><br><span class="line">--name wzbq \</span><br><span class="line">java:openjdk-8u111 java -jar /usr/wzbq/wzbq.jar \</span><br><span class="line"><span class="comment"># 配置</span></span><br><span class="line"><span class="string">&quot;--spring.datasource.url=xxxx&quot;</span> \</span><br><span class="line"><span class="string">&quot;--spring.datasource.username=xxx&quot;</span> \</span><br><span class="line">...</span><br></pre></td></tr></table></figure>
<p>重启成功后能够启动服务，但是无法连接mysql与redis，因为配置中连接用的 <code>localhost</code> ，而java的容器中没有mysql，所以会连接失败，需要容器间网络连接。</p>
<p><strong>另外需要注意的是，此时jar包并不会加载外部的配置文件，而是只加载内部的配置文件。</strong></p>
<h4 id="Docker网络">Docker网络</h4>
<p>默认情况下容器间可进行网络通信，但是非手动配置，用起来不方便。</p>
<p>此时可以创建自定义网络来解决这些问题。把需要互相连通的容器加入网络，容器间就能通过容器名代替其ip地址。</p>
<p>创建网络：</p>
<figure class="highlight bash"><table><tr><td class="code"><pre><span class="line">docker network create 网络名</span><br></pre></td></tr></table></figure>
<p>列出网络：</p>
<figure class="highlight bash"><table><tr><td class="code"><pre><span class="line">docker network <span class="built_in">ls</span></span><br></pre></td></tr></table></figure>
<p>加入网络：</p>
<figure class="highlight bash"><table><tr><td class="code"><pre><span class="line"><span class="comment"># 创建容器时加入</span></span><br><span class="line">docker run --network 网络名 镜像名</span><br><span class="line"><span class="comment"># 容器创建后加入</span></span><br><span class="line">docker network connect 网络名 容器名或<span class="built_in">id</span></span><br><span class="line"><span class="comment"># 如：</span></span><br><span class="line">docker network connect bqNet wzbq</span><br></pre></td></tr></table></figure>
<p>查看网络详情：</p>
<figure class="highlight bash"><table><tr><td class="code"><pre><span class="line">docker network inspect 网络名或<span class="built_in">id</span></span><br></pre></td></tr></table></figure>
<p>删除网络</p>
<figure class="highlight bash"><table><tr><td class="code"><pre><span class="line">docker network <span class="built_in">rm</span> 网络名或<span class="built_in">id</span></span><br></pre></td></tr></table></figure>
<h4 id="前端应用">前端应用</h4>
<p>要求：</p>
<p>使用nginx:1.21.5；</p>
<p>开放端口80；-p</p>
<p>数据卷同步静态资源； -v</p>
<p>重启策略 --restart always</p>
<p>后台运行 -d</p>
<p>容器命名为myNginx</p>
<figure class="highlight bash"><table><tr><td class="code"><pre><span class="line">docker run \</span><br><span class="line">-p 80:80 \</span><br><span class="line">-d --restart always \</span><br><span class="line">-v /usr/wzbqFront:/usr/share/nginx/html \</span><br><span class="line">--name myNginx \</span><br><span class="line">nginx:1.21.5</span><br></pre></td></tr></table></figure>
<h2 id="DockerFile">DockerFile</h2>
<h3 id="基本语法">基本语法</h3>
<p>不区分大小写，但是习惯大写；</p>
<p>使用FROM指令开头</p>
<p>#开头代表注释</p>
<h3 id="快速入门">快速入门</h3>
<h4 id="构建一个最简单的helloworld">构建一个最简单的helloworld</h4>
<ol>
<li class="lvl-3">
<p>创建文件 HelloWorld</p>
</li>
<li class="lvl-3">
<p>编写指令：</p>
</li>
</ol>
<figure class="highlight plaintext"><table><tr><td class="code"><pre><span class="line">FROM centos:7</span><br><span class="line">CMD [&quot;echo&quot;,&quot;hello, world!&quot;]</span><br></pre></td></tr></table></figure>
<ol start="3">
<li class="lvl-3">
<p>编译镜像</p>
</li>
</ol>
<figure class="highlight bash"><table><tr><td class="code"><pre><span class="line">docker build -t hello:1.0 -f HelloWorld .</span><br></pre></td></tr></table></figure>
<p>其中： -t指定镜像名和标签，-f指定dockerFile文件，.标识在当前目录下查找对应的DockerFile来构建镜像。</p>
<ol start="4">
<li class="lvl-3">
<p>运行容器测试</p>
</li>
</ol>
<p>构建完成后，运行容器即可输出hello world：</p>
<figure class="highlight bash"><table><tr><td class="code"><pre><span class="line">docker run hello:1.0</span><br></pre></td></tr></table></figure>
<h3 id="dockerfile-指令学习">dockerfile 指令学习</h3>
<h4 id="FROM">FROM</h4>
<p>作用：用来定义基础镜像</p>
<p>用法：FROM 镜像名：标签名  如 FROM centos:7</p>
<p>作用时机：构建镜像的时候</p>
<h4 id="CMD">CMD</h4>
<p>作用：用来定义容器运行时的默认命令。可以使用docker run的时候覆盖掉CMD中定义的命令。</p>
<p>作用时机：运行容器的时候。</p>
<p>用法：CMD [“命令1”,“参数1”,“参数2”] 如 CMD [“echo”,“hello, world!”] 或 CMD echo $HOME</p>
<p>探索： 在一个DockerFile中写多个CMD，运行效果只会运行最后一个。</p>
<h4 id="ENV">ENV</h4>
<p>需求：基于HelloWorld案例 改造，要求打印内容可以通过环境变量去指定（-e）</p>
<p>用法：ENV 变量名=“变量值”，如ENV DIR=“/root”</p>
<p>作用时机：构建镜像时</p>
<figure class="highlight plaintext"><table><tr><td class="code"><pre><span class="line">FROM centos:7</span><br><span class="line">ENV CONTENT=&quot;hello,world!&quot;</span><br><span class="line">CMD echo $CONTENT</span><br><span class="line"># 或 CMD [&quot;sh&quot;,&quot;-c&quot;,&quot;echo $CONTENT&quot;]</span><br></pre></td></tr></table></figure>
<p>构建后运行容器时：</p>
<figure class="highlight bash"><table><tr><td class="code"><pre><span class="line">docker run -e CONTENT=testModifyEnv --name hello hello:2.0</span><br></pre></td></tr></table></figure>
<p>容器运行后便会输出 “testModifyEnv”</p>
<h4 id="WORKDIR">WORKDIR</h4>
<p>作用：用于设置当前工作的目录，如果该目录不存在会自动创建（包括父目录）。</p>
<p>用法：WORKDIR 目录 如 WORKDIR /app/sg</p>
<p>作用时机：构建镜像时</p>
<h5 id="需求">需求</h5>
<p>定义一个CONTENT变量，默认值为hellodocker，在镜像的/app目录下创建一个sg目录，在其冲创建一个content.txt文件，文件的内容为CONTENT变量的值。容器启动时打印context.txt的内容。</p>
<h5 id="分析">分析</h5>
<p>在linux中：</p>
<figure class="highlight bash"><table><tr><td class="code"><pre><span class="line"><span class="built_in">export</span> CONTENT=hellodocker</span><br><span class="line"><span class="built_in">mkdir</span> /app</span><br><span class="line"><span class="built_in">cd</span> /app</span><br><span class="line"><span class="built_in">mkdir</span> sg</span><br><span class="line"><span class="built_in">cd</span> sg</span><br><span class="line"><span class="built_in">echo</span> <span class="variable">$CONTENT</span> &gt; content.text</span><br><span class="line"><span class="built_in">cat</span> content.txt</span><br></pre></td></tr></table></figure>
<p>转换为dockerfile:</p>
<figure class="highlight plaintext"><table><tr><td class="code"><pre><span class="line">FROM centos:7</span><br><span class="line">ENV CONTENT=&quot;hellodocker&quot;</span><br><span class="line">WORKDIR /app/sg</span><br><span class="line">CMD [&quot;cat&quot;,&quot;content.txt&quot;]</span><br></pre></td></tr></table></figure>
<p>WORKDIR后可以直接引用变量来创建文件夹</p>
<h4 id="RUN">RUN</h4>
<p>作用：用来定义构建过程中要执行的命令</p>
<p>用法：RUN 命令；例如 RUN echo sg</p>
<p>作用时机：构建镜像时</p>
<p>完成上述需求：</p>
<figure class="highlight plaintext"><table><tr><td class="code"><pre><span class="line">FROM centos:7</span><br><span class="line">ENV CONTENT=&quot;hello,docker!&quot;</span><br><span class="line">WORKDIR /app/sg</span><br><span class="line"># 将环境变量值写入到content.txt文件中</span><br><span class="line">RUN echo $CONTENT &gt; content.txt</span><br><span class="line">CMD [&quot;cat&quot;,&quot;content.txt&quot;]</span><br></pre></td></tr></table></figure>
<p>此时用-e修改环境变量时失效，仍是默认值，因为 RUN 在构建镜像时起作用，而-e是启动容器时。</p>
<h4 id="ADD">ADD</h4>
<p>需求：</p>
<p>在构建目录下存放一个sg-blog-vue.tar.gz包，构建镜像时把这个包添加到镜像的/app目录下解压，然后把其中dist目录的内容存放到nginx的html目录下，声明开放80端口。nginx用1.21.5版本。</p>
<p>作用：把构建上下文的文件或网络文件添加到镜像中，如果文件是压缩包，会自动解压。</p>
<p>用法：ADD 源路径 目标路径； 如 ADD dist.tar.gz .</p>
<p>作用时机：构建镜像的时候</p>
<p>完成：</p>
<p>将dist.tar.gz放到docker file的同级目录下后：</p>
<figure class="highlight plaintext"><table><tr><td class="code"><pre><span class="line">FROM nginx:1.21.5</span><br><span class="line">WORKDIR /app</span><br><span class="line"># 压缩包放到根目录下,自动解压，无需再解压</span><br><span class="line">ADD dist.tar.gz .</span><br><span class="line"># 如果时从网络上下载文件，直接写地址即可，但不会自动解压</span><br><span class="line"># ADD url .</span><br><span class="line"># 解压缩 -&gt;  从网络下载的才需要手动解压</span><br><span class="line"># RUN tar -xzvf dist.tar.gz</span><br><span class="line"># 将dist目录下内容放到nginx的html目录下</span><br><span class="line">RUN cp -r dist/* /usr/share/nginx/html</span><br><span class="line">#声明开放端口</span><br><span class="line">EXPOSE 80</span><br><span class="line"># 启动nginx</span><br><span class="line">CMD [&quot;nginx&quot;,&quot;-g&quot;,&quot;daemon off;&quot;]</span><br></pre></td></tr></table></figure>
<p>然后正常构建镜像，运行即可。但是没有开放端口，需要进入容器，curl 127.0.0.1:80 进行测试。</p>
<h4 id="EXPOSE">EXPOSE</h4>
<p>没有声明开放端口的话，用 -p 依然可以开放，但是需要EXPOSE进行声明，以便于他人查看。</p>
<p>作用：暴露需要发布的端口，让镜像使用者指导应该发布哪些端口。</p>
<p>用法：EXPOSE 端口号1 端口号2； 如 EXPOSE 80 8080</p>
<p>作用时机：构建镜像的时候</p>
<h4 id="COPY">COPY</h4>
<p>作用：从构建上下文中复制内容到镜像中，类似于ADD，但只能本地文件，且无法自动解压。</p>
<p>用法：COPY 源路径 目标路径 ； 如 COPY dist.tar.gz .</p>
<p>作用时机：构建镜像时</p>
<h4 id="ENTRYPOINT">ENTRYPOINT</h4>
<p>需求：再上个案例基础上，要求容器的 nginx的命令不能在容器运行时被覆盖</p>
<p>作用：定义容器运行时的默认命令，且docker run 的时候无法覆盖ENTRYPOINT里的内容。</p>
<p>作用时机：运行容器的时候</p>
<p>用法：ENTRYPOINT [“命令1”,“参数1”,“参数2”] ; 如 ENTRYPOINT [“echo”, “hello,world!”]</p>
<p>实际应用经验：</p>
<p>于CMD结合，把不希望被覆盖的命令用ENTRYPOINT定义，其他用CMD定义。</p>
<p>如：</p>
<figure class="highlight plaintext"><table><tr><td class="code"><pre><span class="line">ENTRYPOINT [&quot;java&quot;, &quot;-jar&quot;]</span><br><span class="line">CMD [&quot;app.jar&quot;]</span><br></pre></td></tr></table></figure>
<p>这样 app.jar可覆盖，但是java -jar就无法覆盖</p>
<h2 id="项目部署案例-自定义镜像版本">项目部署案例-自定义镜像版本</h2>
<h3 id="需求-2">需求</h3>
<h4 id="后端">后端</h4>
<p>希望后端的镜像当中就包含了后端的jar包，镜像启动后自动启动该jar包。</p>
<h4 id="前端">前端</h4>
<p>希望前端镜像中就包含了前端的包</p>
<h3 id="实现">实现</h3>
<h4 id="后端-2">后端</h4>
<h5 id="DockerFile-2">DockerFile</h5>
<figure class="highlight plaintext"><table><tr><td class="code"><pre><span class="line">#基础镜像</span><br><span class="line">FROM java:openjdk-8u111</span><br><span class="line"># 添加config配置目录，容器运行时可由数据卷映射真实的配置文件</span><br><span class="line">WORKDIR /app/config</span><br><span class="line">#指定工作目录 注意不能用RUN cd /app 必须用WORKDIR</span><br><span class="line">WORKDIR /app</span><br><span class="line">#添加jar包到镜像中</span><br><span class="line">ADD wzbq.jar .</span><br><span class="line">#声明开放端口</span><br><span class="line">EXPOSE 9988</span><br><span class="line">#指定容器运行命令,后面还可以加相关的启动参数</span><br><span class="line">CMD [&quot;java&quot;,&quot;-jar&quot;,&quot;wzbq.jar&quot;]</span><br></pre></td></tr></table></figure>
<h5 id="运行容器">运行容器</h5>
<figure class="highlight bash"><table><tr><td class="code"><pre><span class="line">docker run -d -p 9988:9988 \</span><br><span class="line">--network bqNet \</span><br><span class="line">--name wzbq \</span><br><span class="line">-v /usr/wzbq/config:/app/config \</span><br><span class="line">--restart always \</span><br><span class="line">wzbq:1.0</span><br></pre></td></tr></table></figure>
<h4 id="前端-2">前端</h4>
<p>前面已经做过。</p>
<h2 id="Docker-Compose">Docker Compose</h2>
<h3 id="下载Docker-Compose">下载Docker Compose</h3>
<figure class="highlight bash"><table><tr><td class="code"><pre><span class="line">sudo curl -L https://github.com/docker/compose/releases/download/1.25.5/docker-compose-`<span class="built_in">uname</span> -s`-`<span class="built_in">uname</span> -m` &gt; /usr/local/bin/docker-compose</span><br><span class="line">sudo <span class="built_in">chmod</span> +x /usr/local/bin/docker-compose</span><br></pre></td></tr></table></figure>
<h3 id="Docker-Compose第一个环境">Docker Compose第一个环境</h3>
<h4 id="两个概念">两个概念</h4>
<p>项目(Project)：有多个服务共同组成的完整业务单元，定义docker-compose.yml文件中；</p>
<p>服务(Service)：一个服务对应一个应用容器，在一个项目中可以存在多个服务</p>
<h4 id="第一个docjer-compose程序">第一个docjer-compose程序</h4>
<p>1.创建一个docker-compose.yml文件</p>
<p>2.在yml中定义多个服务，如：</p>
<figure class="highlight yml"><table><tr><td class="code"><pre><span class="line"><span class="attr">version:</span> <span class="string">&quot;3.0&quot;</span></span><br><span class="line"></span><br><span class="line"><span class="attr">services:</span></span><br><span class="line">  <span class="attr">tomcat:</span> <span class="comment">#服务名 唯一</span></span><br><span class="line">    <span class="attr">image:</span> <span class="string">tomcat:8.0-jre8</span> <span class="comment">#创建当前服务使用的镜像</span></span><br><span class="line">    <span class="attr">ports:</span></span><br><span class="line">     <span class="bullet">-</span> <span class="number">8080</span><span class="string">:8080</span></span><br></pre></td></tr></table></figure>
<p>3.运行docker-compose</p>
<figure class="highlight bash"><table><tr><td class="code"><pre><span class="line">docker-compose up</span><br></pre></td></tr></table></figure>
<h4 id="docker-compose-的命令模板">docker compose 的命令模板</h4>
<figure class="highlight yml"><table><tr><td class="code"><pre><span class="line"><span class="attr">version:</span> <span class="string">&quot;3.0&quot;</span></span><br><span class="line"></span><br><span class="line"><span class="attr">services:</span></span><br><span class="line">  <span class="attr">tomcat:</span> <span class="comment">#服务名称</span></span><br><span class="line">    <span class="attr">container_name:</span> <span class="string">myTomcat</span> <span class="comment">#相当于run的 --name</span></span><br><span class="line">  	<span class="attr">image:</span> <span class="string">tomcat:8.0-jre</span> <span class="comment">#使用哪个镜像 run image</span></span><br><span class="line">  	<span class="attr">ports:</span> <span class="comment">#host与容器的端口映射关系 -p</span></span><br><span class="line">  	  <span class="bullet">-</span> <span class="string">&quot;8080:8080&quot;</span></span><br><span class="line">  	  <span class="bullet">-</span> <span class="string">&quot;8081:8081&quot;</span></span><br><span class="line">  	<span class="attr">volumes:</span> <span class="comment">#宿主机与容器中目录数据共享 -v</span></span><br><span class="line">  	  <span class="bullet">-</span> <span class="string">/root/apps:/usr/local/tomcat/webapps</span> <span class="comment">#使用自定义路径映射</span></span><br><span class="line">  	  <span class="bullet">-</span> <span class="string">tomcatwebapps:/usr/local/tomcat/webapps</span> <span class="comment">#自动创建数据卷，需要在下面声明</span></span><br><span class="line">  	<span class="attr">networks:</span> <span class="comment">## --network</span></span><br><span class="line">  	  <span class="bullet">-</span> <span class="string">helloNet</span> <span class="comment">#指定当前服务使用哪个网络</span></span><br><span class="line">  <span class="attr">mysql:</span></span><br><span class="line">    <span class="attr">image:</span> <span class="string">mysql:8.0.23</span></span><br><span class="line">    <span class="attr">container_name:</span> <span class="string">mysql</span></span><br><span class="line">    <span class="attr">ports:</span></span><br><span class="line">      <span class="bullet">-</span> <span class="string">&quot;3306:3306&quot;</span></span><br><span class="line">    <span class="attr">volumes:</span></span><br><span class="line">      <span class="bullet">-</span> <span class="string">mysqldata:/var/lib/mysql</span></span><br><span class="line">      <span class="bullet">-</span> <span class="string">mysqlconf:/etc/mysql</span></span><br><span class="line">    <span class="attr">environment:</span></span><br><span class="line">      <span class="bullet">-</span> <span class="string">MYSQL_ROOT_PASSWORD=root</span></span><br><span class="line">    <span class="attr">networks:</span></span><br><span class="line">      <span class="bullet">-</span> <span class="string">helloNet</span></span><br><span class="line">  <span class="attr">redis:</span></span><br><span class="line">    <span class="attr">image:</span> <span class="string">redis:latest</span></span><br><span class="line">    <span class="attr">container_name:</span> <span class="string">redis</span></span><br><span class="line">    <span class="attr">ports:</span></span><br><span class="line">      <span class="bullet">-</span> <span class="string">&quot;6379:6379&quot;</span></span><br><span class="line">    <span class="attr">volumes:</span></span><br><span class="line">      <span class="bullet">-</span> <span class="string">redisdata:/data</span></span><br><span class="line">    <span class="attr">network:</span></span><br><span class="line">      <span class="bullet">-</span> <span class="string">helloNet</span></span><br><span class="line">    <span class="attr">command:</span> <span class="string">&quot;redis-server --appendonly yes&quot;</span> <span class="comment"># run 镜像后用来覆盖容器内默认命令</span></span><br><span class="line">    </span><br><span class="line"></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="attr">volumes:</span> <span class="comment">#声明上面服务使用的自动创建的数据卷名</span></span><br><span class="line">  <span class="attr">tomcatwebapps:</span> <span class="comment">#声明的卷名，compose自动创建该卷名但是会在前面自动加项目名</span></span><br><span class="line">    <span class="attr">external:</span> <span class="literal">true</span> <span class="comment">#使用自定义卷名（需要自行创建好）</span></span><br><span class="line"></span><br><span class="line"><span class="attr">network:</span> <span class="comment">#定义服务用到的网络桥</span></span><br><span class="line">  <span class="attr">helloNet:</span> <span class="comment">#定义上面用到的网络桥,compose自动创建但是会在前面自动加项目名</span></span><br><span class="line">    <span class="attr">external:</span> <span class="literal">true</span> <span class="comment">#使用自定义网桥（需要自行创建好）</span></span><br></pre></td></tr></table></figure>
</article><div class="post-copyright"><div class="post-copyright__author"><span class="post-copyright-meta">文章作者: </span><span class="post-copyright-info"><a href="https://lienle.gitee.io">Lenl</a></span></div><div class="post-copyright__type"><span class="post-copyright-meta">文章链接: </span><span class="post-copyright-info"><a href="https://lienle.gitee.io/notes/dockerNotes/">https://lienle.gitee.io/notes/dockerNotes/</a></span></div><div class="post-copyright__notice"><span class="post-copyright-meta">版权声明: </span><span class="post-copyright-info">本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" target="_blank">CC BY-NC-SA 4.0</a> 许可协议。转载请注明来自 <a href="https://lienle.gitee.io" target="_blank">LenlBlog</a>！</span></div></div><div class="tag_share"><div class="post-meta__tag-list"><a class="post-meta__tags" href="/tags/Docker/">Docker</a><a class="post-meta__tags" href="/tags/Linux/">Linux</a></div><div class="post_share"><div class="social-share" data-image="https://z4a.net/images/2023/04/22/fcc1d07f3136fe8e818242a6534391c8.md.png" data-sites="facebook,twitter,wechat,weibo,qq"></div><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/butterfly-extsrc/sharejs/dist/css/share.min.css" media="print" onload="this.media='all'"><script src="https://cdn.jsdelivr.net/npm/butterfly-extsrc/sharejs/dist/js/social-share.min.js" defer></script></div></div><div class="post-reward"><div class="reward-button"><i class="fas fa-qrcode"></i> 打赏</div><div class="reward-main"><ul class="reward-all"><li class="reward-item"><a href="/img/wechat.png" target="_blank"><img class="post-qr-code-img" src= "/img/hexo_loading.gif" data-lazy-src="/img/wechat.png" alt="微信"/></a><div class="post-qr-code-desc">微信</div></li><li class="reward-item"><a href="/img/alipay.png" target="_blank"><img class="post-qr-code-img" src= "/img/hexo_loading.gif" data-lazy-src="/img/alipay.png" alt="支付宝"/></a><div class="post-qr-code-desc">支付宝</div></li></ul></div></div><nav class="pagination-post" id="pagination"><div class="prev-post pull-full"><a href="/notes/mysqlInstall/" title="mysql压缩包方式本地安装"><img class="cover" src= "/img/hexo_loading.gif" data-lazy-src="https://z4a.net/images/2023/04/22/fcc1d07f3136fe8e818242a6534391c8.md.png" onerror="onerror=null;src='/img/404.jpg'" alt="cover of previous post"><div class="pagination-info"><div class="label">上一篇</div><div class="prev_info">mysql压缩包方式本地安装</div></div></a></div></nav><hr/><div id="post-comment"><div class="comment-head"><div class="comment-headline"><i class="fas fa-comments fa-fw"></i><span> 评论</span></div></div><div class="comment-wrap"><div><div class="vcomment" id="vcomment"></div></div></div></div></div><div class="aside-content" id="aside-content"><div class="card-widget card-info"><div class="is-center"><div class="avatar-img"><img src= "/img/hexo_loading.gif" data-lazy-src="/img/lenl.png" onerror="this.onerror=null;this.src='/img/friend_404.gif'" alt="avatar"/></div><div class="author-info__name">Lenl</div><div class="author-info__description">心有所向，日复一日，必有精进~</div></div><div class="card-info-data site-data is-center"><a href="/archives/"><div class="headline">文章</div><div class="length-num">17</div></a><a href="/tags/"><div class="headline">标签</div><div class="length-num">25</div></a><a href="/categories/"><div class="headline">分类</div><div class="length-num">8</div></a></div><a id="card-info-btn" target="_blank" rel="noopener" href="https://github.com/vimianma"><i class="fab fa-github"></i><span>View me</span></a><div class="card-info-social-icons is-center"><a class="social-icon" href="https://gitee.com/lienle" target="_blank" title="Gitee"><i class="fa-brands fa-git" style="color: #c71d23;"></i></a><a class="social-icon" href="https://github.com/vimianma" target="_blank" title="Github"><i class="fab fa-github" style="color: #24292e;"></i></a><a class="social-icon" href="mailto:lenl412@163.com" target="_blank" title="Email"><i class="fas fa-envelope" style="color: #4a7dbe;"></i></a></div></div><div class="card-widget card-announcement"><div class="item-headline"><i class="fas fa-bullhorn fa-shake"></i><span>公告</span></div><div class="announcement_content">本站意在记录个人生活中的所学所感所想所获；<br/>心有所向，日复一日，必有精进~~~<br>请多多指教~</div></div><div class="sticky_layout"><div class="card-widget" id="card-toc"><div class="item-headline"><i class="fas fa-stream"></i><span>目录</span><span class="toc-percentage"></span></div><div class="toc-content"><ol class="toc"><li class="toc-item toc-level-2"><a class="toc-link" href="#Docker%E5%AE%89%E8%A3%85"><span class="toc-number">1.</span> <span class="toc-text">Docker安装</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%8D%B8%E8%BD%BD%E5%B7%B2%E5%AE%89%E8%A3%85%E7%9A%84docker"><span class="toc-number">1.1.</span> <span class="toc-text">卸载已安装的docker</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#Linux%E5%B7%A5%E5%85%B7%E5%AE%89%E8%A3%85"><span class="toc-number">1.2.</span> <span class="toc-text">Linux工具安装</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#docker%E5%AE%89%E8%A3%85"><span class="toc-number">1.3.</span> <span class="toc-text">docker安装</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%90%AF%E5%8A%A8docker"><span class="toc-number">1.4.</span> <span class="toc-text">启动docker</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%B5%8B%E8%AF%95docker"><span class="toc-number">1.5.</span> <span class="toc-text">测试docker</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#docker%E9%95%9C%E5%83%8F%E5%8A%A0%E9%80%9F"><span class="toc-number">1.6.</span> <span class="toc-text">docker镜像加速</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E8%BF%90%E8%A1%8Cgitlab%E5%AE%B9%E5%99%A8%E4%BD%93%E9%AA%8C"><span class="toc-number">1.7.</span> <span class="toc-text">运行gitlab容器体验</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#docker%E5%B8%AE%E5%8A%A9%E6%96%87%E6%A1%A3"><span class="toc-number">1.8.</span> <span class="toc-text">docker帮助文档</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%AE%B9%E5%99%A8%E5%92%8C%E9%95%9C%E5%83%8F"><span class="toc-number">2.</span> <span class="toc-text">容器和镜像</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E9%95%9C%E5%83%8F"><span class="toc-number">2.1.</span> <span class="toc-text">镜像</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#%E6%90%9C%E7%B4%A2%E9%95%9C%E5%83%8F"><span class="toc-number">2.1.1.</span> <span class="toc-text">搜索镜像</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E4%B8%8B%E8%BD%BD%E9%95%9C%E5%83%8F"><span class="toc-number">2.1.2.</span> <span class="toc-text">下载镜像</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E5%88%97%E5%87%BA%E9%95%9C%E5%83%8F%E4%BF%A1%E6%81%AF"><span class="toc-number">2.1.3.</span> <span class="toc-text">列出镜像信息</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E5%88%A0%E9%99%A4%E9%95%9C%E5%83%8F"><span class="toc-number">2.1.4.</span> <span class="toc-text">删除镜像</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#Docker%E6%93%8D%E4%BD%9C%E5%B8%B8%E8%A7%84%E6%B5%81%E7%A8%8B"><span class="toc-number">2.2.</span> <span class="toc-text">Docker操作常规流程</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#%E5%88%97%E5%87%BA%E5%AE%B9%E5%99%A8%E4%BF%A1%E6%81%AF"><span class="toc-number">2.2.1.</span> <span class="toc-text">列出容器信息</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E5%88%9B%E5%BB%BA%E5%B9%B6%E8%BF%90%E8%A1%8C%E5%AE%B9%E5%99%A8"><span class="toc-number">2.2.2.</span> <span class="toc-text">创建并运行容器</span></a><ol class="toc-child"><li class="toc-item toc-level-5"><a class="toc-link" href="#%E8%BF%90%E8%A1%8C%E6%96%B9%E5%BC%8F"><span class="toc-number">2.2.2.1.</span> <span class="toc-text">运行方式</span></a></li></ol></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E5%88%A0%E9%99%A4%E5%AE%B9%E5%99%A8"><span class="toc-number">2.2.3.</span> <span class="toc-text">删除容器</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E8%BF%9B%E5%85%A5%E5%AE%B9%E5%99%A8%E6%89%A7%E8%A1%8C%E5%91%BD%E4%BB%A4"><span class="toc-number">2.2.4.</span> <span class="toc-text">进入容器执行命令</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E6%9F%A5%E7%9C%8B%E5%AE%B9%E5%99%A8%E6%97%A5%E5%BF%97"><span class="toc-number">2.2.5.</span> <span class="toc-text">查看容器日志</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E5%AE%B9%E5%99%A8%E6%96%87%E4%BB%B6%E6%8B%B7%E8%B4%9D"><span class="toc-number">2.2.6.</span> <span class="toc-text">容器文件拷贝</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E5%81%9C%E6%AD%A2%E4%B8%8E%E8%BF%90%E8%A1%8C%E5%AE%B9%E5%99%A8"><span class="toc-number">2.2.7.</span> <span class="toc-text">停止与运行容器</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#run%E5%91%BD%E4%BB%A4%E8%AF%A6%E8%A7%A3"><span class="toc-number">2.3.</span> <span class="toc-text">run命令详解</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#%E5%B8%B8%E7%94%A8%E9%80%89%E9%A1%B9"><span class="toc-number">2.3.1.</span> <span class="toc-text">常用选项</span></a><ol class="toc-child"><li class="toc-item toc-level-5"><a class="toc-link" href="#p-%E7%AB%AF%E5%8F%A3%E6%98%A0%E5%B0%84"><span class="toc-number">2.3.1.1.</span> <span class="toc-text">-p 端口映射</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#v-%E6%95%B0%E6%8D%AE%E5%8D%B7"><span class="toc-number">2.3.1.2.</span> <span class="toc-text">-v 数据卷</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#e%E8%AE%BE%E7%BD%AE%E7%8E%AF%E5%A2%83%E5%8F%98%E9%87%8F"><span class="toc-number">2.3.1.3.</span> <span class="toc-text">-e设置环境变量</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#%E2%80%93name%E5%AE%B9%E5%99%A8%E5%91%BD%E5%90%8D"><span class="toc-number">2.3.1.4.</span> <span class="toc-text">–name容器命名</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#%E2%80%93restart%E9%87%8D%E5%90%AF%E7%AD%96%E7%95%A5"><span class="toc-number">2.3.1.5.</span> <span class="toc-text">–restart重启策略</span></a></li></ol></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%85%B6%E4%BB%96%E9%95%9C%E5%83%8F%E5%92%8C%E5%AE%B9%E5%99%A8%E5%91%BD%E4%BB%A4"><span class="toc-number">2.4.</span> <span class="toc-text">其他镜像和容器命令</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#%E9%95%9C%E5%83%8F-2"><span class="toc-number">2.4.1.</span> <span class="toc-text">镜像</span></a><ol class="toc-child"><li class="toc-item toc-level-5"><a class="toc-link" href="#%E6%9F%A5%E7%9C%8B%E9%95%9C%E5%83%8F%E8%AF%A6%E7%BB%86%E4%BF%A1%E6%81%AF"><span class="toc-number">2.4.1.1.</span> <span class="toc-text">查看镜像详细信息</span></a></li></ol></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E5%AE%B9%E5%99%A8"><span class="toc-number">2.4.2.</span> <span class="toc-text">容器</span></a><ol class="toc-child"><li class="toc-item toc-level-5"><a class="toc-link" href="#%E6%9F%A5%E7%9C%8B%E5%AE%B9%E5%99%A8%E5%86%85%E8%BF%9B%E7%A8%8B%E6%83%85%E5%86%B5"><span class="toc-number">2.4.2.1.</span> <span class="toc-text">查看容器内进程情况</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#%E6%9F%A5%E7%9C%8B%E5%AE%B9%E5%99%A8%E8%AF%A6%E7%BB%86%E4%BF%A1%E6%81%AF"><span class="toc-number">2.4.2.2.</span> <span class="toc-text">查看容器详细信息</span></a></li></ol></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%95%B0%E6%8D%AE%E5%8D%B7%E9%AB%98%E7%BA%A7"><span class="toc-number">2.5.</span> <span class="toc-text">数据卷高级</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#%E8%AE%BE%E7%BD%AE%E6%95%B0%E6%8D%AE%E5%8D%B7"><span class="toc-number">2.5.1.</span> <span class="toc-text">设置数据卷</span></a><ol class="toc-child"><li class="toc-item toc-level-5"><a class="toc-link" href="#%E7%BB%9D%E5%AF%B9%E8%B7%AF%E5%BE%84"><span class="toc-number">2.5.1.1.</span> <span class="toc-text">绝对路径</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#%E5%88%AB%E5%90%8D"><span class="toc-number">2.5.1.2.</span> <span class="toc-text">别名</span></a></li></ol></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E5%88%97%E5%87%BA%E6%89%80%E6%9C%89%E6%95%B0%E6%8D%AE%E5%8D%B7"><span class="toc-number">2.5.2.</span> <span class="toc-text">列出所有数据卷</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E6%9F%A5%E7%9C%8B%E6%95%B0%E6%8D%AE%E5%8D%B7%E7%9A%84%E8%AF%A6%E7%BB%86%E4%BF%A1%E6%81%AF"><span class="toc-number">2.5.3.</span> <span class="toc-text">查看数据卷的详细信息</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E5%88%9B%E5%BB%BA%E6%95%B0%E6%8D%AE%E5%8D%B7"><span class="toc-number">2.5.4.</span> <span class="toc-text">创建数据卷</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E5%88%A0%E9%99%A4%E6%95%B0%E6%8D%AE%E5%8D%B7"><span class="toc-number">2.5.5.</span> <span class="toc-text">删除数据卷</span></a></li></ol></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E9%A1%B9%E7%9B%AE%E9%83%A8%E7%BD%B2%E6%A1%88%E4%BE%8B-%E5%9F%BA%E7%A1%80%E7%89%88%E6%9C%AC"><span class="toc-number">3.</span> <span class="toc-text">项目部署案例-基础版本</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E7%8E%AF%E5%A2%83%E5%87%86%E5%A4%87"><span class="toc-number">3.1.</span> <span class="toc-text">环境准备</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#mysql"><span class="toc-number">3.1.1.</span> <span class="toc-text">mysql</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#redis"><span class="toc-number">3.1.2.</span> <span class="toc-text">redis</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%BA%94%E7%94%A8%E9%83%A8%E7%BD%B2"><span class="toc-number">3.2.</span> <span class="toc-text">应用部署</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#%E5%90%8E%E7%AB%AF%E5%BA%94%E7%94%A8"><span class="toc-number">3.2.1.</span> <span class="toc-text">后端应用</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#Docker%E7%BD%91%E7%BB%9C"><span class="toc-number">3.2.2.</span> <span class="toc-text">Docker网络</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E5%89%8D%E7%AB%AF%E5%BA%94%E7%94%A8"><span class="toc-number">3.2.3.</span> <span class="toc-text">前端应用</span></a></li></ol></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#DockerFile"><span class="toc-number">4.</span> <span class="toc-text">DockerFile</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%9F%BA%E6%9C%AC%E8%AF%AD%E6%B3%95"><span class="toc-number">4.1.</span> <span class="toc-text">基本语法</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%BF%AB%E9%80%9F%E5%85%A5%E9%97%A8"><span class="toc-number">4.2.</span> <span class="toc-text">快速入门</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#%E6%9E%84%E5%BB%BA%E4%B8%80%E4%B8%AA%E6%9C%80%E7%AE%80%E5%8D%95%E7%9A%84helloworld"><span class="toc-number">4.2.1.</span> <span class="toc-text">构建一个最简单的helloworld</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#dockerfile-%E6%8C%87%E4%BB%A4%E5%AD%A6%E4%B9%A0"><span class="toc-number">4.3.</span> <span class="toc-text">dockerfile 指令学习</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#FROM"><span class="toc-number">4.3.1.</span> <span class="toc-text">FROM</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#CMD"><span class="toc-number">4.3.2.</span> <span class="toc-text">CMD</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#ENV"><span class="toc-number">4.3.3.</span> <span class="toc-text">ENV</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#WORKDIR"><span class="toc-number">4.3.4.</span> <span class="toc-text">WORKDIR</span></a><ol class="toc-child"><li class="toc-item toc-level-5"><a class="toc-link" href="#%E9%9C%80%E6%B1%82"><span class="toc-number">4.3.4.1.</span> <span class="toc-text">需求</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#%E5%88%86%E6%9E%90"><span class="toc-number">4.3.4.2.</span> <span class="toc-text">分析</span></a></li></ol></li><li class="toc-item toc-level-4"><a class="toc-link" href="#RUN"><span class="toc-number">4.3.5.</span> <span class="toc-text">RUN</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#ADD"><span class="toc-number">4.3.6.</span> <span class="toc-text">ADD</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#EXPOSE"><span class="toc-number">4.3.7.</span> <span class="toc-text">EXPOSE</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#COPY"><span class="toc-number">4.3.8.</span> <span class="toc-text">COPY</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#ENTRYPOINT"><span class="toc-number">4.3.9.</span> <span class="toc-text">ENTRYPOINT</span></a></li></ol></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E9%A1%B9%E7%9B%AE%E9%83%A8%E7%BD%B2%E6%A1%88%E4%BE%8B-%E8%87%AA%E5%AE%9A%E4%B9%89%E9%95%9C%E5%83%8F%E7%89%88%E6%9C%AC"><span class="toc-number">5.</span> <span class="toc-text">项目部署案例-自定义镜像版本</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E9%9C%80%E6%B1%82-2"><span class="toc-number">5.1.</span> <span class="toc-text">需求</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#%E5%90%8E%E7%AB%AF"><span class="toc-number">5.1.1.</span> <span class="toc-text">后端</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E5%89%8D%E7%AB%AF"><span class="toc-number">5.1.2.</span> <span class="toc-text">前端</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%AE%9E%E7%8E%B0"><span class="toc-number">5.2.</span> <span class="toc-text">实现</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#%E5%90%8E%E7%AB%AF-2"><span class="toc-number">5.2.1.</span> <span class="toc-text">后端</span></a><ol class="toc-child"><li class="toc-item toc-level-5"><a class="toc-link" href="#DockerFile-2"><span class="toc-number">5.2.1.1.</span> <span class="toc-text">DockerFile</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#%E8%BF%90%E8%A1%8C%E5%AE%B9%E5%99%A8"><span class="toc-number">5.2.1.2.</span> <span class="toc-text">运行容器</span></a></li></ol></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E5%89%8D%E7%AB%AF-2"><span class="toc-number">5.2.2.</span> <span class="toc-text">前端</span></a></li></ol></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#Docker-Compose"><span class="toc-number">6.</span> <span class="toc-text">Docker Compose</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E4%B8%8B%E8%BD%BDDocker-Compose"><span class="toc-number">6.1.</span> <span class="toc-text">下载Docker Compose</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#Docker-Compose%E7%AC%AC%E4%B8%80%E4%B8%AA%E7%8E%AF%E5%A2%83"><span class="toc-number">6.2.</span> <span class="toc-text">Docker Compose第一个环境</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#%E4%B8%A4%E4%B8%AA%E6%A6%82%E5%BF%B5"><span class="toc-number">6.2.1.</span> <span class="toc-text">两个概念</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E7%AC%AC%E4%B8%80%E4%B8%AAdocjer-compose%E7%A8%8B%E5%BA%8F"><span class="toc-number">6.2.2.</span> <span class="toc-text">第一个docjer-compose程序</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#docker-compose-%E7%9A%84%E5%91%BD%E4%BB%A4%E6%A8%A1%E6%9D%BF"><span class="toc-number">6.2.3.</span> <span class="toc-text">docker compose 的命令模板</span></a></li></ol></li></ol></li></ol></div></div><div class="card-widget card-recent-post"><div class="item-headline"><i class="fas fa-history"></i><span>最新文章</span></div><div class="aside-list"><div class="aside-list-item"><a class="thumbnail" href="/notes/dockerNotes/" title="Docker备忘笔记"><img src= "/img/hexo_loading.gif" data-lazy-src="https://z4a.net/images/2023/04/22/fcc1d07f3136fe8e818242a6534391c8.md.png" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="Docker备忘笔记"/></a><div class="content"><a class="title" href="/notes/dockerNotes/" title="Docker备忘笔记">Docker备忘笔记</a><time datetime="2024-04-06T13:17:09.000Z" title="发表于 2024-04-06 21:17:09">2024-04-06</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/notes/mysqlInstall/" title="mysql压缩包方式本地安装"><img src= "/img/hexo_loading.gif" data-lazy-src="https://z4a.net/images/2023/04/22/fcc1d07f3136fe8e818242a6534391c8.md.png" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="mysql压缩包方式本地安装"/></a><div class="content"><a class="title" href="/notes/mysqlInstall/" title="mysql压缩包方式本地安装">mysql压缩包方式本地安装</a><time datetime="2024-01-03T05:33:33.000Z" title="发表于 2024-01-03 13:33:33">2024-01-03</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/notes/PdfView/" title="基于pdf.js的前后端分离式PDF文件预览实现"><img src= "/img/hexo_loading.gif" data-lazy-src="https://z4a.net/images/2023/04/22/fcc1d07f3136fe8e818242a6534391c8.md.png" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="基于pdf.js的前后端分离式PDF文件预览实现"/></a><div class="content"><a class="title" href="/notes/PdfView/" title="基于pdf.js的前后端分离式PDF文件预览实现">基于pdf.js的前后端分离式PDF文件预览实现</a><time datetime="2023-08-06T02:18:26.000Z" title="发表于 2023-08-06 10:18:26">2023-08-06</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/notes/electronVue/" title="Electron+Vue项目搭建"><img src= "/img/hexo_loading.gif" data-lazy-src="https://z4a.net/images/2023/04/22/fcc1d07f3136fe8e818242a6534391c8.md.png" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="Electron+Vue项目搭建"/></a><div class="content"><a class="title" href="/notes/electronVue/" title="Electron+Vue项目搭建">Electron+Vue项目搭建</a><time datetime="2023-05-14T02:36:59.000Z" title="发表于 2023-05-14 10:36:59">2023-05-14</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/notes/chunkFile/" title="基于SpringBoot与Vue的大文件分片上传与流式下载"><img src= "/img/hexo_loading.gif" data-lazy-src="https://z4a.net/images/2023/04/22/fcc1d07f3136fe8e818242a6534391c8.md.png" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="基于SpringBoot与Vue的大文件分片上传与流式下载"/></a><div class="content"><a class="title" href="/notes/chunkFile/" title="基于SpringBoot与Vue的大文件分片上传与流式下载">基于SpringBoot与Vue的大文件分片上传与流式下载</a><time datetime="2023-05-13T13:57:28.000Z" title="发表于 2023-05-13 21:57:28">2023-05-13</time></div></div></div></div></div></div></main><footer id="footer" style="background: -#484847"><div id="footer-wrap"><div class="copyright">&copy;2023 - 2024 By Lenl</div><div class="framework-info"><span>框架 </span><a target="_blank" rel="noopener" href="https://hexo.io">Hexo</a><span class="footer-separator">|</span><span>主题 </span><a target="_blank" rel="noopener" href="https://github.com/jerryc127/hexo-theme-butterfly">Butterfly</a></div><div class="footer_custom_text">Hi, welcome to my blog !</div></div></footer></div><div id="rightside"><div id="rightside-config-hide"><button id="readmode" type="button" title="阅读模式"><i class="fas fa-book-open"></i></button><button id="darkmode" type="button" title="浅色和深色模式转换"><i class="fas fa-adjust"></i></button><button id="hide-aside-btn" type="button" title="单栏和双栏切换"><i class="fas fa-arrows-alt-h"></i></button></div><div id="rightside-config-show"><button id="rightside_config" type="button" title="设置"><i class="fas fa-cog fa-spin"></i></button><button class="close" id="mobile-toc-button" type="button" title="目录"><i class="fas fa-list-ul"></i></button><a id="to_comment" href="#post-comment" title="直达评论"><i class="fas fa-comments"></i></a><button id="go-up" type="button" title="回到顶部"><span class="scroll-percent"></span><i class="fas fa-arrow-up"></i></button></div></div><div><script src="/js/utils.js"></script><script src="/js/main.js"></script><script src="https://cdn.jsdelivr.net/npm/@fancyapps/ui/dist/fancybox/fancybox.umd.min.js"></script><script src="https://cdn.jsdelivr.net/npm/instant.page/instantpage.min.js" type="module"></script><script src="https://cdn.jsdelivr.net/npm/vanilla-lazyload/dist/lazyload.iife.min.js"></script><div class="js-pjax"><script>function loadValine () {
  function initValine () {
    const valine = new Valine(Object.assign({
      el: '#vcomment',
      appId: 'dBlOeCLNhBxbSdZO44fRKdBa-gzGzoHsz',
      appKey: 'DCSQoDhPl0UOF7Ix9Imdfd9X',
      avatar: 'monsterid',
      serverURLs: '',
      emojiMaps: "",
      path: window.location.pathname,
      visitor: false
    }, null))
  }

  if (typeof Valine === 'function') initValine() 
  else getScript('https://cdn.jsdelivr.net/npm/valine/dist/Valine.min.js').then(initValine)
}

if ('Valine' === 'Valine' || !false) {
  if (false) btf.loadComment(document.getElementById('vcomment'),loadValine)
  else setTimeout(loadValine, 0)
} else {
  function loadOtherComment () {
    loadValine()
  }
}</script></div><script defer="defer" id="ribbon" src="https://cdn.jsdelivr.net/npm/butterfly-extsrc/dist/canvas-ribbon.min.js" size="150" alpha="0.6" zIndex="-1" mobile="false" data-click="true"></script><script async data-pjax src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script><div id="local-search"><div class="search-dialog"><nav class="search-nav"><span class="search-dialog-title">搜索</span><span id="loading-status"></span><button class="search-close-button"><i class="fas fa-times"></i></button></nav><div class="is-center" id="loading-database"><i class="fas fa-spinner fa-pulse"></i><span>  数据库加载中</span></div><div class="search-wrap"><div id="local-search-input"><div class="local-search-box"><input class="local-search-box--input" placeholder="搜索文章" type="text"/></div></div><hr/><div class="no-result" id="local-search-results"></div><div id="local-search-stats-wrap"></div></div></div><div id="search-mask"></div><script src="/js/search/local-search.js"></script></div></div></body></html>